package com.ruoyi.project.AnswerQuestion.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.AnswerQuestion.domain.TbQuestion;
import com.ruoyi.project.AnswerQuestion.service.ITbQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 实验答疑，提问Controller
 * 
 * @author lvyongwei
 * @date 2020-11-26
 */
@Controller
@RequestMapping("/system/question")
public class TbQuestionController extends BaseController
{
    private String prefix = "system/question";

    @Autowired
    private ITbQuestionService tbQuestionService;

    //@RequiresPermissions("system:question:view")
    @GetMapping("/look")
    public String question(int experimentId,ModelMap modelMap)
    {
        /**
         * 容器存储实验id
         */
        modelMap.addAttribute("experimentId",experimentId);
        modelMap.addAttribute("userId",getSysUser().getUserId());
        return prefix + "/question";
    }

    /**
     * 查询实验答疑，提问列表
     */
    //@RequiresPermissions("system:question:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbQuestion tbQuestion)
    {
        startPage();
        List<TbQuestion> list = tbQuestionService.selectTbQuestionList(tbQuestion);
        return getDataTable(list);
    }

    /**
     * 导出实验答疑，提问列表
     */
    //@RequiresPermissions("system:question:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbQuestion tbQuestion)
    {
        List<TbQuestion> list = tbQuestionService.selectTbQuestionList(tbQuestion);
        ExcelUtil<TbQuestion> util = new ExcelUtil<TbQuestion>(TbQuestion.class);
        return util.exportExcel(list, "question");
    }

    /**
     * 新增实验答疑，提问
     */
    @GetMapping("/add")
    public String add(Integer experimentId,ModelMap modelMap)
    {
        modelMap.addAttribute("experimentId",experimentId);
        return prefix + "/add";
    }

    /**
     * 新增保存实验答疑，提问
     */
    //@RequiresPermissions("system:question:add")
    @Log(title = "实验答疑，提问", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbQuestion tbQuestion)
    {
        Long userId = getSysUser().getUserId();
        tbQuestion.setQuestionUserId(userId);
        return toAjax(tbQuestionService.insertTbQuestion(tbQuestion));
    }

    /**
     * 修改实验答疑，提问
     */
    @GetMapping("/edit/{questionId}")
    public String edit(@PathVariable("questionId") Long questionId, ModelMap mmap)
    {
        TbQuestion tbquestion = tbQuestionService.selectTbQuestionById(questionId);
        mmap.put("tbQuestion", tbquestion);
        return prefix + "/edit";
    }

    /**
     * 修改保存实验答疑，提问
     */
    //@RequiresPermissions("system:question:edit")
    @Log(title = "实验答疑，提问", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbQuestion tbQuestion)
    {
        return toAjax(tbQuestionService.updateTbQuestion(tbQuestion));
    }

    /**
     * 删除实验答疑，提问
     */
    //@RequiresPermissions("system:question:remove")
    @Log(title = "实验答疑，提问", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbQuestionService.deleteTbQuestionByIds(ids));
    }


    /**
     * 教师回复时跳转到页面
     * @param experimentId
     * @param modelMap
     * @return
     */
    @GetMapping("/look2")
    public String questions(int experimentId,ModelMap modelMap)
    {
        /**
         * 容器存储实验id
         */
        modelMap.addAttribute("experimentId",experimentId);
        modelMap.addAttribute("userId",getSysUser().getUserId());
        return prefix + "/questionAnswer";
    }
}
